const express = require("express");
const {createBundleRenderer} = require("vue-server-renderer")
const app = express();
const resolve = dir => require("path").resolve(__dirname,dir)
//加载静态文件。使服务器可以读取文件,不读取index.html文件
app.use(express.static(resolve("../dist/client"),{index:false}))
//加载打包好的服务端清单
const bundle = resolve('../dist/server/vue-ssr-server-bundle.json');
//按照服务端的清单进行渲染
const renderer = createBundleRenderer(bundle,{
  runInNewContext:false,
  template:require("fs").readFileSync(resolve('../public/index.html'),'utf-8'),//这里引入html页面模板
  clientManifest:require(resolve('../dist/client/vue-ssr-client-manifest.json'))
});
app.get("*",async (req,res)=>{
  const context = {
    title:"打你个大西瓜",
    url:req.url
  }
  const html = await renderer.renderToString(context)
  res.send(html)
})

app.listen(3000,()=>{
  console.log("server start")
})